home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / c1.zip / LEJLIB.C < prev    next >
Text File  |  1987-06-18  |  11KB  |  394 lines

  1. /* The following are Microsoft c libraries to be included at compile time */
  2. #include <stdio.h>                              /* std I/O library on c disk */
  3.  
  4. /* The following are global definitions to be used throughout the program */
  5. #define EOF -1                                  /* logical ^Z EOF ends copy */
  6. #define YES 1
  7. #define NO 0
  8. #define NL '\n'
  9. #define EOS '\0'
  10. #define MAXLINE 1024
  11. #define SIZ 9
  12. #define FF '\014'                               /* Form feed */
  13. #define BELL '\007'
  14. #define DIGMAX 10
  15. #define CLEAR 12                                /* ASCII form feed */
  16.  
  17. /* The following are external variables that can be used in any functions */
  18. int inword;
  19. int nl;
  20. int nw;
  21. int nc;
  22. int ndigits[DIGMAX];
  23. int c;
  24.  
  25.  
  26.  
  27.  
  28. /* ---------------------------------------------------------------------- */
  29.  
  30.  
  31. main()                                /*                                    */
  32. {
  33.  
  34.         int len;                        /* current line length */
  35.         int maxlen;                        /* max length so far */
  36.         char line[MAXLINE];
  37.         char save[MAXLINE];
  38.         char lineupper[MAXLINE];
  39.         char linelower[MAXLINE];
  40.         char answer[MAXLINE];
  41.  
  42.         printf(FF);
  43.         printf("\n");
  44.         printf("\n");
  45.         printf("Jordan's function test ...\n");
  46.         printf("\n");
  47.  
  48.  
  49.  
  50.         inchar("Input password .....\b\b\b\b\b",5,answer);
  51.         printf("\n\nThe password is %s\n", answer);
  52.  
  53. }
  54.  
  55.  
  56.  
  57. /* ---------------------------------------------------------------------- */
  58.  
  59.  
  60. redirect()                                     /* redirect keybd to CRT */
  61. {
  62.         int c;
  63.  
  64.         printf("\n");
  65.         printf("\n");
  66.         printf("Jordan's redirect function...\n");
  67.         printf("\n");
  68.         printf("Input numbers, space characters, etc...\n");
  69.         printf("?");
  70.  
  71.  
  72.         while ((c = getchar()) != EOF)         /* get char until EOF found */
  73.                 putchar(c);                    /* send char to scrn: */
  74. }
  75.  
  76.  
  77. /* ---------------------------------------------------------------------- */
  78.  
  79.  
  80.  
  81. wordcnt()                             /* count lines, words, chars in input */
  82. {
  83.  
  84.         double c, nc, nl, nw;                         /* counters are float */
  85.         int inword;                                   /* logical is interger */
  86.  
  87.         inword = NO;
  88.         nl = nw = nc = 0.0;                           /* initialize variables */
  89.  
  90.         printf("\n");
  91.         printf("\n");
  92.         printf("Jordan's word count function ...\n");
  93.         printf("\n");
  94.         printf("Input text for character, word and line count search.\n");
  95.         printf("\n");
  96.  
  97.  
  98.         while ((c = getchar()) != EOF) {
  99.                 nc = nc + 1;
  100.  
  101.                 if (c == '\n')                         /* if c=LF then inc nl */
  102.                      nl = nl + 1;
  103.  
  104.                 if (c == ' ' || c == '\n' || c == '\t')  /* if char is space, */
  105.                      inword = NO;                       /* LF or TAB then it */
  106.                                                         /* cannot be a word */
  107.  
  108.                 else if (inword == NO) {                /* otherwise it is a */
  109.                      inword = YES;                      /* word */
  110.                      nw = nw + 1;
  111.                 }
  112.         }
  113.         printf("\n");
  114.         printf("\n");
  115.         printf("Number of characters = ");
  116.         printf("%.0f\n",nc);                       /* prt float # with no dec */
  117.         printf("Number of words = ");
  118.         printf("%.0f\n",nw);
  119.         printf("Number of lines = ");
  120.         printf("%.0f\n",nl);
  121.  
  122.         printf("\n");
  123. }
  124.  
  125.  
  126. /* ---------------------------------------------------------------------- */
  127.  
  128.  
  129. maxline()                       /* gets maximum length line in file */
  130. {
  131.         int len;                        /* current line length */
  132.         int maxlen;                        /* max length so far */
  133.         char line[MAXLINE];
  134.         char save[MAXLINE];
  135.  
  136.         maxlen = 0;
  137.  
  138.         printf("\n");
  139.         printf("\n");
  140.         printf("Jordan's get maximum length line function ...\n");
  141.         printf("\n");
  142.         printf("Input text for maximum line length search.\n");
  143.         printf("\n");
  144.  
  145.  
  146.         while ((len = getline(line, MAXLINE)) > 0)   /* getline defined below */
  147.                 if (len > maxlen){
  148.                         maxlen = len;
  149.                         copy(line, save);              /* function from below */
  150.                 }
  151.         if (maxlen > 0)
  152.             printf("%s", save);
  153. }
  154.  
  155.  
  156. /* ---------------------------------------------------------------------- */
  157.  
  158.  
  159. getline(s, lim)                 /* getline into s, return length of s */
  160. char s[];
  161. int lim;
  162. {
  163.         int c,i;
  164.  
  165.         for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
  166.                 s[i] = c;
  167.         if (c == '\n'){
  168.                 s[i] = c;
  169.                 ++i;
  170.         }
  171.         s[i] = '\0';
  172.         return(i);
  173. }
  174.  
  175.  
  176.  
  177. /* ---------------------------------------------------------------------- */
  178.  
  179.  
  180. copy(s1, s2)                    /* copy s1 to s2; assume s2 big enough */
  181. char s1[], s2[];
  182. {
  183.         int i;
  184.  
  185.         i = 0;
  186.         while (s2[i] = s1[i] != EOS)
  187.                 ++i;
  188. }
  189.  
  190.  
  191.  
  192.  
  193. /* ---------------------------------------------------------------------- */
  194.  
  195.  
  196. power(x, n)                         /* raise x to n-th power; n > 0 */
  197. int x, n;
  198. {
  199.         int i, p;
  200.  
  201.         p = 1;
  202.         for (i=1; i<=n; ++i)
  203.             p=p*x;
  204.         return(p);
  205. }
  206.  
  207.  
  208.  
  209. /* ---------------------------------------------------------------------- */
  210.  
  211.  
  212. digicnt()                                /* count digits, spaces, others */
  213. {
  214.         int c, i, nspaces, nother;       /* integers */
  215.         int ndigits[DIGMAX];             /* integer array */
  216.  
  217.         nspaces = nother = 0;
  218.         for (i= 0; i < 10; ++i)
  219.                 ndigits[i] = 0;          /* initialize arry to 0 */
  220.  
  221.  
  222.         printf("\n");
  223.         printf("\n");
  224.         printf("Jordan's number counter...\n");
  225.         printf("\n");
  226.         printf("Input numbers, space characters, etc...\n");
  227.         printf("?");
  228.  
  229.         while ((c = getchar()) != EOF)
  230.                 if (c >= '0' && c <= '9')
  231.                       ++ndigits[c];
  232.                 else if (c == ' ' || c == '\n' || c == '\t')
  233.                         ++nspaces;
  234.                 else
  235.                         ++nother;
  236.  
  237.  
  238.  
  239.         printf("\n");
  240.         printf("\n");
  241.         printf("Jordan's number counter results...\n");
  242.         printf("\n");
  243.         printf("Number of digits = ");
  244.         for (i=0; i < 10; ++i)
  245.                printf(" %d", ndigits[i]);
  246.         printf("\nNumber of spaces = %d,  Other = %d\n",
  247.                nspaces, nother);
  248.         printf("\n");
  249. }
  250.  
  251.  
  252.  
  253. /* ---------------------------------------------------------------------- */
  254.  
  255. strlen(s)                               /*  return length of string s */
  256. char s[];
  257. {
  258.         int i;
  259.  
  260.         i = 0;
  261.         while (s[i] != '\0')
  262.                 ++i;
  263.         return(i);
  264. }
  265.  
  266.  
  267. /* ---------------------------------------------------------------------- */
  268.  
  269. atoi(s)                         /* convert numeric string into numeric equiv */
  270. char s[];
  271. {
  272.  
  273.         int i;
  274.         int n;                /* need double precision for no > 32K */
  275.  
  276.         n = 0;
  277.         for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
  278.               n = 10 * n + s[i] - '0';
  279.  
  280.         return(n);
  281. }
  282.  
  283.  
  284. /* ---------------------------------------------------------------------- */
  285.  
  286.  
  287. upper(s)                        /*  converts lower case to upper */
  288. char s[];
  289. {
  290.         int i, j;
  291.  
  292.         for (i = j = 0; s[i] != '\0'; i++,j++)          /* inc i after using it */
  293.         if (s[i] >= 'a' && s[i] <= 'z')             /* if upeer case do next */
  294.                 s[j] = s[i] + 'A' - 'a';
  295.         else
  296.                 s[j] = s[i];
  297.  
  298.         s[++j] = '\0';
  299.  
  300. }
  301.  
  302.  
  303. /* ---------------------------------------------------------------------- */
  304.  
  305.  
  306. lower(s)                        /*  converts upper case to lower */
  307. char s[];
  308. {
  309.         int i, j;
  310.  
  311.         for (i = j = 0; s[i] != '\0'; i++)        /* inc i after using it */
  312.         if (s[i] >= 'A' && s[i] <= 'Z')           /* if upeer case do next */
  313.                 s[j++] = s[i] + 'a' - 'A';          /* convert ASCII value */
  314.         else
  315.                 s[j++] = s[i];                      /* only copy as is */
  316.  
  317.         s[j++] = '\0';
  318.  
  319. }
  320.  
  321.  
  322.  
  323.  
  324. /* ---------------------------------------------------------------------- */
  325.  
  326.  
  327. isdigit(c)                      /* test character to see if it is a digit */
  328. int c;
  329. {
  330.         int isdigit;
  331.  
  332.         return(isdigit = c >= '0' && c<= '9');  /* isdigit true = 1 */
  333.                                                 /* isdigit false = 0 */
  334.  
  335. }
  336.  
  337.  
  338. /* ---------------------------------------------------------------------- */
  339.  
  340.  
  341. stuf(s)                         /* stuffs string s with letters A-I */
  342. char s[];
  343. {
  344.         int i,x;
  345.  
  346.         for(x = 65, i = 0; i < SIZ; ++x, ++i)
  347.                 s[i] = x;
  348. }
  349.  
  350.  
  351. /* ---------------------------------------------------------------------- */
  352.                          /* prints mid string 'stradr' beggining at  */
  353.                          /* 'start' and going for 'num' characters */
  354. mid(stradr,start,num)
  355. int start, num;
  356. char stradr[];
  357. {
  358.         int i;
  359.  
  360.         for(i = 0; i < num; ++i)
  361.                 printf("%c", stradr[start + i]);
  362. }
  363.  
  364.  
  365. /* ---------------------------------------------------------------------- */
  366.                                 /*  prompts for answer of 'num' chars */
  367.                                 /*  then returns 'answer' to main */
  368.  
  369. inchar(prompt,num,answer)
  370. char *prompt, answer[];
  371. int num;
  372. {
  373.         int j;
  374.  
  375.         printf("\n%s", prompt);
  376.  
  377.         for(j = 0; j < num; ++j)
  378.                 answer[j] = getchar();
  379.         answer[num] = EOS;
  380.  
  381.         return(answer);
  382. }
  383.  
  384.  
  385. /* ---------------------------------------------------------------------- */
  386. /* ---------------------------------------------------------------------- */
  387. /* ---------------------------------------------------------------------- */
  388. /* ---------------------------------------------------------------------- */
  389. /* ---------------------------------------------------------------------- */
  390. /* ---------------------------------------------------------------------- */
  391. /* ---------------------------------------------------------------------- */
  392. /* ---------------------------------------------------------------------- */
  393. /* ---------------------------------------------------------------------- */
  394.